package com.linedata.acq.metier.impl;

import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.TGSqlParser;

import java.io.File;
import java.io.IOException;

import com.linedata.acq.metier.GestionNorme;
import com.linedata.acq.metier.norme.asql.IASQL_DREF;
import com.linedata.acq.utils.plsql.utils;

public class GestionNormeImpl implements GestionNorme {

	// Logging
//	private static final Log	LOG			= LogFactory.getLog(ASQL_DREFTest.class);

	// Spring Beans
	private IASQL_DREF			asql_dref;

	public IASQL_DREF getAsql_dref() {
		return asql_dref;
	}

	public void setAsql_dref(IASQL_DREF asql_dref) {
		this.asql_dref = asql_dref;
	}

	// Declarations
	private String				typeFile[]	= { "TRG", "PRC", "PKG", "FUN", "SCRPT", "SEQU",
			"INDX", "VIEW", "TABL"			};
	private String				type		= typeFile[4];
	private String				s			= "./src/main/resources/plsql/" + type + ".sql";
	private String				sourceWF;
	private TGSqlParser			sqlparser;

	public GestionNormeImpl() {

		this.sqlparser = new TGSqlParser(EDbVendor.dbvoracle);

		File file = new File(s);
		if (!file.exists()) {
			System.out.println("File not exists:" + s);
			return;
		}

		try {
			sourceWF = utils.fileWithoutFooter(file, type, utils.fileFooter(file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/**
		 * GSP
		 */
		// TODO Faire une condition si la syntaxe n'est pas valide

		sqlparser.sqltext = sourceWF;

		int ret = sqlparser.parse();
		if (ret != 0) {
			System.out.println(sqlparser.getErrormessage());
			System.out.println("Corrigez la syntaxe pour pouvoir lancer le moteur");
			return;
		}
	}

	public void process() {

		asql_dref.codeREFDEV(sqlparser);
	}

	@Override
	public String toString() {
		return asql_dref.toString();
	}
}
